package com.KKd.controller;

import com.KKd.pojo.Blog;
import com.KKd.pojo.Comment;
import com.KKd.service.BlogService;
import com.KKd.service.CommentService;
import com.KKd.utils.JsonUtils;
import com.KKd.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 14587
 * @date 2021/3/15 17:03
 */
@Controller
public class CommController {
    @Autowired
    CommentService commentService;
    @Autowired
    BlogService blogService;

    @RequestMapping("/showCommBlog/{blogId}")
    //根据博客id进行评论评论功能
    public String showCommBlog(@PathVariable int blogId, Model model){
        List<Comment> comments = commentService.queryComments(blogId);
        //剩下的应该在前端进行数据的选择，倘若有commCommId，那么说明是回复的，就用c:if进行判断
        //然后使用不同的颜色体，这里在查询的时候就要默认排序，按照相同commUserId的放在一起，进行输出，
        //意思就是把有回复的从第二条开始的内容变成回复的格式，同时回复的人也能在前端进行输出，有就输出回复@
        //+commUserId，没有就不显示
        //数据库排序需要按照commUserId和回复时间和置顶，也就是0或者1进行排序
        model.addAttribute(comments);
        return "blog";
    }
    //这里单独用ajax返回也是可以的，控制这个下标，直接返回，所以不用加请求
    @RequestMapping("/reply/{commUserId}/{blogId}/{comment}")
    @ResponseBody
    public String reply(@PathVariable String commUserId,@PathVariable int blogId,@PathVariable String comment){
        String commCommId = UUIDUtils.getUUID();
        String date = JsonUtils.getJson(new Date());
        int modify = commentService.addComment(new Comment(0, commCommId, commUserId, blogId, date, comment, 0, false));
        if(modify>0) return "评论成功";
        return "回复失败";
    }

    @RequestMapping("/setFirstComm/{commId}/{blogId}")
    @ResponseBody
    public String setFirstComm(@PathVariable int commId,@PathVariable int blogId){
        int modifyMss = commentService.setCommentFirstById(commId);
        Blog blog = blogService.queryBlog((Map) new HashMap().put("blogId", blogId));
        if(modifyMss>0)return "设置成功";//这里也需要前端进行调整，因为前面设置的格式这里不容易拼接
        return "设置失败";
    }
}
